/*
package com.example.demo.test;

import com.jlefebure.spring.boot.minio.MinioConfigurationProperties;
import com.jlefebure.spring.boot.minio.MinioService;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.jlefebure.spring.boot.minio.MinioConfigurationProperties;
import com.jlefebure.spring.boot.minio.MinioService;
import io.minio.messages.Item;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

*/
/**
 * @author 贺超
 * @date 2021/2/19 11:20
 *//*

@RestController
public class MinioUtil {

    @Autowired
    MinioService minioService;

    @Autowired
    private MinioConfigurationProperties configurationProperties;

    @GetMapping("/files")
    public List<Item> testMinio() {
        return minioService.list();
    }

    */
/**
     * 根据文件名称下载文件
     *
     * @param object
     * @param response
     * @throws com.jlefebure.spring.boot.minio.MinioException
     * @throws IOException
     * @throws IOException
     *//*

    @GetMapping("files/{object}")
    public void getObject(@PathVariable("object") String object, HttpServletResponse response) throws com.jlefebure.spring.boot.minio.MinioException, IOException, IOException {
        InputStream inputStream = minioService.get(Paths.get(object));
        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);

        // Set the content type and attachment header.
        response.addHeader("Content-disposition", "attachment;filename=" + object);
        response.setContentType(URLConnection.guessContentTypeFromName(object));
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();
    }

    @PostMapping("/uploads")
    public List<String> addAttachements(HttpServletRequest request) throws IOException {
        List<MultipartFile> fileList = ((MultipartHttpServletRequest) request).getFiles("file");
        if (fileList.isEmpty()) {
            return null;
        }
        List<String> resultFilePath = new LinkedList();
        String url = configurationProperties.getUrl() + "/" + configurationProperties.getBucket();

        fileList.forEach(multipartFile -> {
            try {

                String fileName = multipartFile.getOriginalFilename();
                String uuid = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
                Path path = Paths.get(uuid);
                minioService.upload(path, multipartFile.getInputStream(), multipartFile.getContentType());

                //添加接口地址
                resultFilePath.add(url + "/" + uuid);
            } catch (com.jlefebure.spring.boot.minio.MinioException e) {
                System.out.println("无法在内部存储上上传文件。请稍后重试");
            } catch (IOException e) {
                System.out.println("无法读取文件");
            }
        });

        return resultFilePath;
    }

    @PostMapping("/upload")
    public String addAttachement(HttpServletRequest request) throws IOException {
        List<MultipartFile> fileList = ((MultipartHttpServletRequest) request).getFiles("file");
        if (fileList.isEmpty()) {
            return null;
        }
        try {
            String url = configurationProperties.getUrl() + "/" + configurationProperties.getBucket();
            MultipartFile multipartFile = fileList.get(0);
            String fileName = multipartFile.getOriginalFilename();
            String uuid = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
            Path path = Paths.get(uuid);
            minioService.upload(path, multipartFile.getInputStream(), multipartFile.getContentType());
            //添加接口地址
            return url + "/" + uuid;
        } catch (
                com.jlefebure.spring.boot.minio.MinioException e) {
            System.out.println("无法在内部存储上上传文件。请稍后重试");
        } catch (IOException e) {
            System.out.println("无法读取文件");
        }
        return null;
    }

}
*/
